#include "MyProgressBar.h"
#include "ui_MyProgressBar.h"
#include "ClientManager.h"
#include "Define.h"

#include <QPalette>

MyProgressBar::MyProgressBar(QWidget *parent) :
    QWidget(parent),
    m_ui(new Ui::MyProgressBar)
{
    m_ui->setupUi(this);
}

MyProgressBar::MyProgressBar(FileKey key, long long size, QWidget *parent) :
	QWidget(parent),
	m_ui(new Ui::MyProgressBar)
{
	m_ui->setupUi(this);
	_key = key;
	_size = size;
	if (_size == 0l)
		_size = 1000l;
}

MyProgressBar::~MyProgressBar()
{
    delete m_ui;
}

void MyProgressBar::Initialize()
{
	_fin = false;
	m_ui->progressBar->setMaximum(_size);/////////////
	m_ui->progressBar->setValue(0);
	m_ui->pushButton->setDisabled(true);

	QString name, group;
	if (!CLIENT_MANAGER->GetClientInfo(_key.GetAddress(), name, group))
	{
		name = "unknown";
		group = "unknown";
	}
	QString mode = (_key.GetMode() == Rx ? "Rx" : "Tx");
	m_ui->label->setText(mode + ' ' + group + ':' + name + ' ' + _key.GetFileName());

	QPalette pal = this->palette();
	pal.setColor(QPalette::Highlight, _key.GetMode() == Tx ? Qt::red : Qt::blue);
	this->setPalette(pal);
}

void MyProgressBar::SetValue(int size)
{
	m_ui->progressBar->setValue(m_ui->progressBar->value() + size);
}

void MyProgressBar::EnableButton()
{
	m_ui->pushButton->setEnabled(true);
	m_ui->progressBar->setValue(_size);
	_fin = true;
}

void MyProgressBar::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void MyProgressBar::on_button_stop_clicked()
{
//	emit sigDestroy(_addr, _fileName, _mode, _size);
//	emit sigDestroy(FileKey(_fileName, _addr, _mode), _size);
}

void MyProgressBar::on_pushButton_clicked()
{
	if (_fin)
		emit sigDestroyProgressBar(_key);
}
